﻿using System;
using System.Linq;
using SlamCms.Data;

namespace SlamCms.Web
{
	public class DefaultSlamCmsUserResolver : ISlamCmsUserResolver
	{
		public User GetUser(SlamCmsContext slamCmsContext)
		{
			if (!slamCmsContext.HttpContext.User.Identity.IsAuthenticated)
				return null;

			var userName = slamCmsContext.HttpContext.User.Identity.Name;

			int userId;

			if (int.TryParse(userName, out userId))
				return slamCmsContext.Data.Users[userId];
			else
				return slamCmsContext.Data.Users.FirstOrDefault(x => x.UserName.Equals(userName, StringComparison.InvariantCultureIgnoreCase));
		}
	}
}
